// Fill out your copyright notice in the Description page of Project Settings.


#include "MyCrossHairWidget.h"
#include "Components/Image.h"
#include "Blueprint/WidgetLayoutLibrary.h"
#include "Components/CanvasPanelSlot.h"

void UMyCrossHairWidget::NativeConstruct()
{
	Super::NativeConstruct();

	UTexture2D* DefaultTexture = LoadObject<UTexture2D>(nullptr, TEXT("/Game/Image/CharmCrosshair_Big"));
	if (DefaultTexture)
	{
		CrossHair_Image_Big = DefaultTexture;

		UE_LOG(LogTemp, Warning, TEXT("Image : Charm Cross Hair Big is Loaded!"));
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, TEXT("Image : Charm Cross Hair Big is Loaded!"));
		}
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Image : Charm Cross Hair Big is Not Loaded!"));
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, TEXT("Image : Charm Cross Hair Big is Not Loaded!"));
		}
	}
	
	UTexture2D* HitTexture = LoadObject<UTexture2D>(nullptr, TEXT("/Game/Image/CharmCrosshair_Small"));
	if(HitTexture)
	{
		CrossHair_Image_Small = HitTexture;

		UE_LOG(LogTemp, Warning, TEXT("Image : Charm Cross Hair Small is Loaded!"));
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, TEXT("Image : Charm Cross Hair Small is Loaded!"));
		}
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Image : Charm Cross Hair Small is Not Loaded!"));
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, TEXT("Image : Charm Cross Hair Small is Not Loaded!"));
		}
	}
}

void UMyCrossHairWidget::UpdateCrossHair(bool HitSpecialTarget)
{
	if (HitSpecialTarget)
	{
		if (CrossHairImage && CrossHair_Image_Small)
		{
			CrossHairImage->SetBrushFromTexture(this->CrossHair_Image_Small);

			CrossHairImage->SetRenderScale(FVector2D(0.5f, 0.5f));

			GetWorld()->GetTimerManager().SetTimer(
				ResetTimerHandle,
				this,
				&UMyCrossHairWidget::ResetCrossHair,
				0.15f,
				false
			);

			UE_LOG(LogTemp, Warning, TEXT("Hit Special Target, the Cross Hair is become small"));
		}
	}
	else
	{
		if (CrossHairImage && CrossHair_Image_Small)
		{
			CrossHairImage->SetBrushFromTexture(this->CrossHair_Image_Big);
		}
	}
}

void UMyCrossHairWidget::ResetCrossHair()
{
	if (CrossHairImage && CrossHair_Image_Big)
	{
		GetWorld()->GetTimerManager().ClearTimer(this->ResetTimerHandle);

		CrossHairImage->SetBrushFromTexture(this->CrossHair_Image_Big);

		CrossHairImage->SetRenderScale(FVector2D(1.0f, 1.0f));


		UE_LOG(LogTemp, Warning, TEXT("Time Manager is to the End, Reset the Cross Hair Image"));
	}
}

FVector2D UMyCrossHairWidget::GetCrossHairScreenPosition()
{
	if (CrossHairImage) 
	{
		return UWidgetLayoutLibrary::SlotAsCanvasSlot(CrossHairImage)->GetPosition();
	}

	return FVector2D::ZeroVector; 
}

